package com.flashcash.solu.view.camera;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.flashcash.kotlin.data.FileType;
import com.flashcash.solu.R;
import com.flashcash.solu.app.base.BaseActivity;
import com.flashcash.solu.view.camera.presenter.TakePhotoActPreImp;
import com.flashcash.solu.view.camera.presenter.TakePhotoActPresenter;
import com.flashcash.solu.widget.xleotoast.XLeoToast;
import com.flurgle.camerakit.CameraListener;
import com.flurgle.camerakit.CameraView;
import com.hwangjr.rxbus.RxBus;
import com.tbruyelle.rxpermissions.RxPermissions;

import rx.Subscriber;

public class TakePhotoActivity extends BaseActivity<TakePhotoActPresenter> implements TakePhotoActView {
    public static final String EXTRA_FILE_TYPE = "extra_file_type";
    private FileType fileType;

    private RelativeLayout relativeLayout;
    private CameraView cameraView;

    private View mCamerCover;

    private ImageButton btnCapture;
    private ImageView mbtnCancel;

    public static void actionStart(Context context, FileType fileType) {
        Intent intent = new Intent(context, TakePhotoActivity.class);
        intent.putExtra(EXTRA_FILE_TYPE, fileType);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.activity_take_photo;
    }

    @Override
    protected void init() {
        RxBus.get().register(this);

        Intent intent = getIntent();
        fileType = (FileType) intent.getSerializableExtra(EXTRA_FILE_TYPE);

        mCamerCover = findViewById(R.id.imageview_id_frame);
        /*if (FileType.KTP_PHOTO.ordinal() != fileType.ordinal()) {
            mCamerCover.setVisibility(View.GONE);
        } else if (mCamerCover.getVisibility() != View.VISIBLE) {
            mCamerCover.setVisibility(View.VISIBLE);
        }*/

        relativeLayout = (RelativeLayout) findViewById(R.id.containerImg);
        cameraView = (CameraView) findViewById(R.id.cameraView);
        cameraView.setCameraListener(new CameraListener() {
            @Override
            public void onPictureTaken(byte[] picture) {
                super.onPictureTaken(picture);

                mPresenter.onTaken(picture, fileType);
            }
        });


        mbtnCancel = findViewById(R.id.button_cancel);
        mbtnCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(Activity.RESULT_CANCELED);
                finish();
            }
        });


        btnCapture = (ImageButton) findViewById(R.id.button_shoot);
        btnCapture.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                btnCapture.setClickable(false);
                showLoading(getString(R.string.loading_storing_img));
                cameraView.captureImage();
            }
        });
    }

    private void checkCurrentPermission() {
        new RxPermissions(this)
                .request(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE})
                .subscribe(new Subscriber<Boolean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable throwable) {
                        XLeoToast.showMessage(R.string.error_occured);
                        setResult(Activity.RESULT_CANCELED);
                        finish();
                    }

                    @Override
                    public void onNext(Boolean aBoolean) {
                        Log.d("permissionResult:", "" + aBoolean);
                        if (aBoolean) {
                            cameraView.start();
                        } else {
                            new AlertDialog.Builder(TakePhotoActivity.this)
                                    .setTitle(R.string.permission_denied)
                                    .setCancelable(false)
                                    .setMessage(String.format(getString(R.string.need_permission), Manifest.permission.CAMERA.replace("android.permission.", "")
                                            + "、" + Manifest.permission.WRITE_EXTERNAL_STORAGE.replace("android.permission.", "")))
                                    .setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                            setResult(Activity.RESULT_CANCELED);
                                            TakePhotoActivity.this.finish();
                                        }
                                    })
                                    .show();
                        }
                    }
                });
    }

    @Override
    protected TakePhotoActPresenter initPresenterImpl() {
        return new TakePhotoActPreImp();
    }

    @Override
    protected void onStart() {
        super.onStart();
        checkCurrentPermission();
    }

    @Override
    protected void onStop() {
        cameraView.stop();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unregister(this);
        super.onDestroy();
    }
}
